<?php

namespace app\home\controller;

use app\home\logic\MemberLogic;
use app\home\model\MemberModel;
use org\Verify;
use taobao\AliSms;
use think\Cookie;
use think\Db;
use think\File;
use think\Request;
use think\Session;


class Member extends Base
{
    private $user_id;
    public function __construct(Request $request = null)
    {
        $action = strtolower( Request::instance()->action());
        if(!in_array($action,['login','registe','re_user','checkverify','regok','re_user_ok'])) {
            $logic = new MemberLogic();
            $logic->check_logic();
            if(!$logic->result){
                $this->error($logic->msg,url('login'));
            }
        }
        $this->user_id = Session::get('daguan_u_id');
        parent::__construct($request);
    }

    //需要重新写

    /**

     * index [ 会员中心界面 ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function index()
    {
        $model = new MemberModel();
        $user = $model->field('head_img,brithday,mobile,nickname,email,sex,truename,integral,money,update_time')->where(['id'=>$this->user_id])->find();
        $user = $user->toArray();
        $user['brithday'] = date('Y-m-d',$user['brithday']);
        $this->assign('user',$user);
        return view();
    }
    public function update(){
        if (request()->isAjax()) {
            $param = input('param.');
            $result = [];
            $result['msg'] = '';
            $data = [];
            if(isset($param['nickname']) && $param['nickname'] == ''){
                $result['msg'] .= "昵称不能为空";
                return $result;
            }else{
                $data['nickname'] = $param['nickname'];
            }
            if(isset($param['mobile']) && is_mobile($param['mobile'])){
                $data['mobile'] = $param['mobile'];
            }else{
                $result['msg'] .= "手机号码格式不对";
                return $result;
            }
            if(isset($param['email']) && filter_var($param['email'],FILTER_VALIDATE_EMAIL)){
                $data['email'] = $param['email'];
            }else{
                $result['msg'] .= "邮箱格式不对";
                return $result;
            }
            if(isset($param['sex'])){
                $data['sex'] = $param['sex'];
            }
            if(isset($param['truename'])){
                $data['truename'] = $param['truename'];
            }
            if(isset($param['brithday'])){
                $data['brithday'] = strtotime($param['brithday']);
            }
            if(isset($param['head_img'])){
                $data['head_img'] = $param['head_img'];
            }
            if(isset($param['password']) && strlen($param['password']) < 6){
                $result['msg'] .= "密码不能少于6位";
            }else{
                $data['password'] = md5(md5($param['password']));
            }
            $MemberModel = New MemberModel();
            $MemberModel->update($data,['id'=>$this->user_id]);
            if($result['msg'] == ''){
                $result['msg'] = '更新成功';
            }
            return $result;
        }
    }


    /**
     * login [ 会员登录 ]
     * @author [Boot.Z] [852952656@qq.com]
     *
     */
    public function login()
    {
        if (request()->isAjax()){
            $param = input('param.');
            $MemberModel = New MemberModel();
            return  $MemberModel ->vaildateUser($param);
        }else{
            $logic = new MemberLogic();
            $logic->check_logic();
            if($logic->result === false){//sessionû��¼
                $uid = Cookie::get('daguan_u_id');
                $password = Cookie::get('daguan_password');
                $MemberModel = New MemberModel();
                $param['id'] = $uid;
                $param['password'] = $password;
                $param['remember'] = 0;
                $result = $MemberModel ->vaildateUser($param,true);
                if($result['code'] == 1){
                    $this->redirect('Index/index');
                }
            }else{
                $this->error('您已经登录了',url('index/index'));
            }
            return $this->fetch();
        }

    }



    /**

     * register [ 会员注册功能 ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function registe()

    {



        if (request()->isAjax()) {

            $param = input('param.');

            $mobile = $param['phone'];

            $UserModel = Db::name('member');

            if ($UserModel->where(array('account' => $mobile))->find()) {

                $res['code'] = 2;

                $res['msg'] = "此账户已存在";

                return json($res);

            }



            $code = $param['code'];

            if ($this->check_verify($code, $id = '')) {

                $res['code'] = 1;

                $res['msg'] = "短信验证码发送成功";

                $this->AliSms($mobile);



                return json($res);

            } else {

                $res['code'] = 0;

                $res['msg'] = "验证码错误";



                return json($res);

            }



        } else {


            $logic = new MemberLogic();
            $logic->check_logic();
            if($logic->result === false){
                return $this->fetch();
            }else{
                $this->error('您已经登录了，请注销后再注册',url('index/index'));
            }

        }

    }



    /**

     * register [ 会员注册功能 ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function regok()

    {

        if (request()->isAjax()){

            $param = input('param.');

            if ($param['alicode'] !==  cookie::get('code')){

                $res['code'] = 0;

                $res['msg'] ="短信验证失败";

                return json($res);

            }else{

                $UserModel = Db::name('member');

                $data['account'] = $param['phone'];

                $data['password'] = md5(md5($param['pwd1']));

                $data['nickname'] = $param['nickname'];

                $data['create_time'] = time();

                $data['update_time'] = time();

                $data['group_id'] = 122;

                if ($UserModel ->insert($data)){

                    $res['code'] = 1;

                    $res['msg'] ="注册成功";

                    $res['msg'] ="注册成功";

                    return json($res);

                }else{

                    $res['code'] = 0;

                    $res['msg'] ="注册失败";

                    return json($res);

                }

            }

        }else{

            return $this->fetch();

        }

    }





    /**

     * re_user [ 找回密码 ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function re_user()

    {

        if (request()->isAjax()) {

            $param = input('param.');

            $mobile = $param['phone'];

            $UserModel = Db::name('member');

            // dump($UserModel);die;

            if (!$UserModel->where(array('account' => $mobile))->find()) {

                $res['code'] = 2;

                $res['msg'] = "此账户不存在";

                return json($res);

            }



            $code = $param['code'];

            if ($this->check_verify($code, $id = '')) {

                $res['code'] = 1;

                $res['msg'] = "短信验证码发送成功";

                $this->AliSms($mobile);



                return json($res);

            } else {

                $res['code'] = 0;

                $res['msg'] = "验证码错误";



                return json($res);

            }



        } else {



            return $this->fetch();

        }

    }



    /**

     * re_user_ok [ 找回密码成功 ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function re_user_ok()

    {

        if (request()->isAjax()){

            $param = input('param.');

            if ($param['alicode'] !==  cookie::get('code')){

                $res['code'] = 0;

                $res['msg'] ="短信验证失败";

                return json($res);

            }else{

                $UserModel = Db::name('member');

                //$data['account'] = $param['phone'];

                $data['password'] = md5(md5($param['pwd1']));

                $data['update_time'] = time();

                $data['group_id'] = 122;

                if ($UserModel ->where(array('account' => $param['phone']))->update($data)){

                    $res['code'] = 1;

                    $res['msg'] ="找回密码成功";

                    return json($res);

                }else{

                    $res['code'] = 0;

                    $res['msg'] ="找回密码失败";

                    return json($res);

                }

            }

        }else{

            return $this->fetch();

        }

    }





    /**

     * logout [ 注销会员功能 ]

     * @author [Boot.Z] [852952656@qq.com]

     */
    public function logout()
    {
        Cookie::clear('daguan_');
        Session::clear();
        $this->success('退出成功',url('index/index'));
    }



    /**

     * checkVerify [ ��֤ ]

     * @author [Boot.Z] [852952656@qq.com]

     */

    public function checkverify()

    {

        $verify = new Verify();

        $verify->imageH = 32;

        $verify->imageW = 100;

        $verify->codeSet = '0123456789';

        $verify->length = 4;

        $verify->useNoise = false;

        $verify->fontSize = 15;

        $verify->entry();



    }



    /**


     * check_verify [ 验证码验证 ]

     * @author [Boot.Z] [852952656@qq.com]

     * @return mixed

     * @param $code

     * @param string $id


     */

    private function check_verify($code, $id = ''){

        $captcha = new Verify();

        return $captcha->check($code, $id);

    }



    /**

     * AliSms [ 阿里大于发短信 ]

     * @author [Boot.Z] [852952656@qq.com]

     * @param $mobile

     */

    public function AliSms($mobile)

    {

        $code = rand (111111,999999);

        Cookie::set('code',$code,3600);

        $AliSms = New AliSms();

        $is_ok =  $AliSms ->send_verify($mobile, $code);

    }

    public function head_upload(){
        $file = request()->file('file');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'heads');
            if($info){
                $result = [];
                $result['path'] = DS . 'heads'.DS.$info->getSaveName();
                return json_encode($result);
            }else{
                return json_encode($file->getError());
            }
        }
    }















}

